<?php

/**
 * This is the model class for table "cattle".
 *
 * The followings are the available columns in table 'cattle':
 * @property string $id
 * @property string $tokenNumber
 * @property string $purchaseDate
 * @property string $purchaseMandi
 * @property string $ownerName
 * @property string $purchasePrice
 * @property string $saleDate
 * @property integer $salePrice
 * @property string $saleTo
 *
 * The followings are the available model relations:
 * @property Cattlemilk[] $cattlemilks
 * @property Cattletreatment[] $cattletreatments
 */
class Cattle extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cattle';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('purchaseDate, purchaseMandi, ownerName, salePrice, saleTo', 'required'),
			array('salePrice', 'numerical', 'integerOnly'=>true),
			array('tokenNumber, purchasePrice', 'length', 'max'=>11),
			array('purchaseMandi, ownerName', 'length', 'max'=>100),
			array('saleDate', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, tokenNumber, purchaseDate, purchaseMandi, ownerName, purchasePrice, saleDate, salePrice, saleTo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cattlemilks' => array(self::HAS_MANY, 'Cattlemilk', 'cattleUid'),
			'cattletreatments' => array(self::HAS_MANY, 'Cattletreatment', 'cattleUid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tokenNumber' => 'Token Number',
			'purchaseDate' => 'Purchase Date',
			'purchaseMandi' => 'Purchase Mandi',
			'ownerName' => 'Owner Name',
			'purchasePrice' => 'Purchase Price',
			'saleDate' => 'Sale Date',
			'salePrice' => 'Sale Price',
			'saleTo' => 'Sale To',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('tokenNumber',$this->tokenNumber,true);
		$criteria->compare('purchaseDate',$this->purchaseDate,true);
		$criteria->compare('purchaseMandi',$this->purchaseMandi,true);
		$criteria->compare('ownerName',$this->ownerName,true);
		$criteria->compare('purchasePrice',$this->purchasePrice,true);
		$criteria->compare('saleDate',$this->saleDate,true);
		$criteria->compare('salePrice',$this->salePrice);
		$criteria->compare('saleTo',$this->saleTo,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Cattle the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
